package cheng.zefan.basic.exceptrion;


import cheng.zefan.basic.result.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * <p>全局异常处理类</p>
 *
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 该接口只处理空指针异常
     * @param
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    public JSONResult NpeHandler(NullPointerException e){
        e.printStackTrace();
        log.error(e.getMessage());
        return JSONResult.error("空指针异常");
    }

    /**
     * 该接口只处理算术异常
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    public JSONResult arithmeticExceptionHandler(ArithmeticException e){
        e.printStackTrace();
        log.error(e.getMessage());
        return JSONResult.error("土皮提示：发生算术异常了");
    }

    /**
     * 该接口只处理自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(MyException.class)
    public JSONResult myExceptionHandler(MyException e){
        e.printStackTrace();
        log.error(e.getMessage());
        return JSONResult.error(e.getMessage());
    }

    /**
     * 兜底异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public JSONResult exceptionHandler(Exception e){
        e.printStackTrace();
        log.error(e.getMessage());
        return JSONResult.error("土皮提示：系统开小差了，请稍候再试");
    }
}